/*
** Copyright (c) 2010  Kimmo 'Rainy' Pekkola
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program.  If not, see http://www.gnu.org/licenses.
*/

#include "mainwindow.h"
#include "graphview.h"
#include <QtSql>
#include "databasemanager.h"

//-----------------------------------------------------------------------------
/**
** Constructor
**
** \param pParent The parent for the main window (usually NULL)
*/
CMainWindow::CMainWindow(QWidget* pParent)
    : QMainWindow(pParent)
{
    setWindowTitle("Investments");

#ifdef Q_WS_MAEMO_5
    setAttribute(Qt::WA_Maemo5StackedWindow);
#endif
}

//-----------------------------------------------------------------------------
/**
** Destructor
*/
CMainWindow::~CMainWindow()
{
    CDatabaseManager::free();
}

//-----------------------------------------------------------------------------
/**
** Initializes the widgets.
*/
void CMainWindow::initialize()
{
    CDatabaseManager::instance().initialize();

    QWidget* pRoot = new QWidget(this);

    QGridLayout* pLayout = new QGridLayout();
    pLayout->setContentsMargins(20, 20, 20, 20);
    pLayout->setSpacing(20);

    int count = 4;
    int cols = sqrt(count + 1);
    for (int i = 0; i < count; i++)
    {
        QString strName = QString("Graph%1").arg(i + 1);

        QSettings settings;
        settings.beginGroup(strName);
        int fundId = settings.value("id", -1).toInt();
        QString fundName = settings.value("name").toString();
        settings.endGroup();

        CGraphView* pGraph = new CGraphView(pRoot, fundName, fundId);
        pGraph->setObjectName(strName);
        pLayout->addWidget(pGraph, i / cols, i % cols);

/*
        bool bOk = false;
        bOk = connect(pVolume, SIGNAL(clicked(QString)), this, SLOT(onScanVolume(QString)));
        Q_ASSERT(bOk);
*/    }

    pRoot->setLayout(pLayout);
    setCentralWidget(pRoot);

    // Create menu items
    QAction* aboutAction = new QAction(tr("About"), this);

    // Add item into menu
#if defined(Q_WS_MAEMO_5)
    menuBar()->addAction(aboutAction);
#else
    // else File menu
    QMenu* menu = new QMenu(tr("File"), this);
    menu->addAction(aboutAction);

    QAction* exitAction = new QAction(tr("Exit"), this);
    menu->addAction(exitAction);
    connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));

    menuBar()->addMenu(menu);
#endif

    connect(aboutAction, SIGNAL(triggered()), this, SLOT(onAbout()));

    setContextMenuPolicy(Qt::DefaultContextMenu);
}

///////////////////////////////////////////////////////////////////////////////
/// OVERRIDES
///////////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------------
/**
** Shows the context menu. Shown only if the mouse is over one of the widgets.
*/
void CMainWindow::contextMenuEvent(QContextMenuEvent* pEvent)
{
    CGraphView* pGraphWidget = qobject_cast<CGraphView*>(centralWidget()->childAt(pEvent->pos()));
    if (pGraphWidget)
    {
        QMenu menu(this);

        QList< QPair<QString, int> > listNames = CDatabaseManager::instance().names();
        for (int i = 0; i < listNames.size(); i++)
        {
            QAction* pAction = new QAction(listNames[i].first, this);
            pAction->setData(listNames[i].second);
            menu.addAction(pAction);
        }

        QAction* pAction = menu.exec(pEvent->globalPos());
        if (pAction)
        {
            QSettings settings;
            settings.beginGroup(pGraphWidget->objectName());
            settings.setValue("name", pAction->text());
            settings.setValue("id", pAction->data());
            settings.endGroup();

            pGraphWidget->setTitle(pAction->text());
        }
    }
}

///////////////////////////////////////////////////////////////////////////////
/// SLOTS
///////////////////////////////////////////////////////////////////////////////

//-----------------------------------------------------------------------------
/**
** Slot which gets called when the menu item is selected. Opens the about dialog.
*/
void CMainWindow::onAbout()
{
    QString strText = tr("Investments by Kimmo Pekkola");
    QMessageBox::information(this, tr("Investments"), strText);
}

// EOF
